<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
require_once("../Scripts/DatabaseConnect.php"); 
session_start();
require_once("../Scripts/UpdateStocks.php");//Checks if we need to update the database with new market data.

//Check if we need local server or remote server
$con=Remoteconnect();
if($con==null){
	$con=Localconnect();	
}
//Display the last update date and current date and time.
$lastUpdate="";
$result = mysqli_query($con,"SELECT * FROM  `time`");

while($row = mysqli_fetch_array($result)) {
	$lastUpdate=$row['LastUpdate'];

  
}

$date = date('m-d-Y h:i:s a', time());//gets current date and time of server.
echo"Date: $date, Last Update: $lastUpdate";
?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN" dir="ltr">
<head profile="http://gmpg.org/xfn/11">
<title>J&J Free Stock backtesting</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="imagetoolbar" content="no" />
<link rel="stylesheet" href="businesstoday/styles/layout.css" type="text/css" />
</head>
<body id="top">
<div class="wrapper col1">
  <div id="header">
    <img src="../Images/the-top-5-high-volume-trading-stocks-of-the-week_1118_437849_0_14042581_500.jpg" alt="" width="240" height="162" />
    <div id="logo">
      <h1>J&amp;J Free Stock Backtesting    </h1>
    </div>
<div id="newsletter">
    <div id="login" style="display:block; min-height:50px; min-width:600px;">
      <p><a href="#">Lost your password?</a> <a href="register.php">Register</a>

      </p>
      
      <form action="../Scripts/authenticate.php" method="post" >
      
        <fieldset>
          <legend>NewsLetter</legend>
          <table>
            <tr>
              <td>Name:</td>
              <td><input type="email" name="email" required="required" id="email" /></td>
              <td>Password:</td>
              <td><input type="password" name="password" required="required" id="password" /></td>
              <td><input type="submit" name="news_go" id="news_go" value="Login" /></td>
            </tr>
          </table>
        </fieldset>
      </form>


      </div>
      
      <?php
	  //Wrong password message
      if (isset($_SESSION['msg'])) {
	echo "<p style='color:red'>".$_SESSION['msg']."</p>"; 
	//remove the message since it has been displayed.
	unset($_SESSION['msg']);
	}
	?>
    
   	<?php
	//logout button
    if (isset($_SESSION['msgG'])) {
		echo"<form>";
		echo "<p style='color:green'>".$_SESSION['msgG']."</p>"; 
		echo '<input type="button" name="logout" id="logout" value="Logout" onClick="location.href=\'../Scripts/Logout.php\'"/>'; 
		echo"</form>";
	}
	
?> 
  

    </div>
    <br class="clear" />
  </div>
</div>

<div class="wrapper col2">
  <div id="topbar">
    <div id="topnav">
      <ul>
        <li class="active">
         
              <li><a href="index.php">Home Page</a></li>
              <li id="portfolio" style="display:none"><a href="portfolio.php">Stock Portfolio</a></li>
              <li id="stockMonitoring" style="display:none"><a href="stockMonitoring.php">Stock Monitoring</a></li>
              <li><a href="stockbacktest.php">Stock Backtest</a></li>
              <li><a href="charts.php">Stock Charts</a>
               <ul>
            <li><a href="chartDifShares.php">Stock Prices Comparision Chart</a></li>
            <li><a href="chartIndicators.php">Stock Technical Analysis Chart</a></li>
         
          </ul>
        </li>

        
  </li>      
        
        
      </ul>
    </div>
 <script type="text/javascript">
 //Hides/Shows navigation bar links based on logged in status
	  var check = <?php echo json_encode(isset($_SESSION['email'])); ?>;
	  
	  if(check==true){
		  document.getElementById("login").style.display="none";
		  document.getElementById("portfolio").style.display="block";
		  document.getElementById("stockMonitoring").style.display="block";

	  }else{
		  document.getElementById("login").style.display="block";
		  document.getElementById("stockMonitoring").style.display="none";
		  document.getElementById("portfolio").style.display="none";
		  
	  }
	 
 </script>
 
       
    <br class="clear" />
  </div>
</div>
<div class="wrapper col3">

  <div id="intro">
    <div class="fl_right"></div>
    <br class="clear" />
  </div>
</div>
<div class="wrapper col4"></div>
<div class="wrapper col5">
  <div id="container">
    <div id="content" style="max-height:1000px; min-width:1050px; overflow:auto;">
      <h2>Results From The Great Back Testing Algorithm~!</h2>
      <?php
	  
require_once("../Scripts/DatabaseConnect.php");
require_once("../Scripts/PeclStrats.php");//Holds all of the pecl trader strategies functions

//stoping the page from timeing out
set_time_limit(130);
//Database connection
$con = Remoteconnect();
if ($con == null) {
    $con = Localconnect();
}
	//Variables to send to javascript to graph output
	$dates=array();
	$numberOfSharesArray=array();
	$profitLossArray=array();
	$gainArray=array();
	$signalsArray=array();
	
if ($_REQUEST['Backtest']) {

    //Initial Setup of variables from form
    $Symbol = $_POST['Symbol'];
    $StartingCapital = $_POST['StartingCapital'];
    $TradingCommission = $_POST['TradingCommission'];
    $StopLong = $_POST['StopLong'];
    $LongTarget = $_POST['LongTarget'];
    $StopLongInput = $_POST['StopLongInput'];
    $LongTargetInput = $_POST['LongTargetInput'];
    $StartDate = $_POST['StartDate'];
    $EndDate = $_POST['EndDate'];
	$LockSize=$_POST['LockSize']; // Minimum number of shares you can buy and sell

    //this variable is used to calculate the overal profit at the end of the backtesting process
    $BeginningCapital = $StartingCapital;

    //First buy signal
    $Buy1_1 = $_POST['Buy1_1'];
    $Buy1_2 = $_POST['Buy1_2'];
    $Buy1_3 = $_POST['Buy1_3'];
    $BuyInput1_1 = $_POST['BuyInput1_1'];
    $BuyInput1_2 = $_POST['BuyInput1_2'];
    $BuyInput1_3 = $_POST['BuyInput1_3'];

    //First sell signal
    $Sell1_1 = $_POST['Sell1_1'];
    $Sell1_2 = $_POST['Sell1_2'];
    $Sell1_3 = $_POST['Sell1_3'];
    $SellInput1_1 = $_POST['SellInput1_1'];
    $SellInput1_2 = $_POST['SellInput1_2'];
    $SellInput1_3 = $_POST['SellInput1_3'];
	
	//Stop Long indicator
	$StopIndicatorSell1_1 = $_POST['StopIndicatorSell1_1'];
	$StopIndicatorSell1_2 = $_POST['StopIndicatorSell1_2'];
	$StopIndicatorSell1_3 = $_POST['StopIndicatorSell1_3'];
	$StopIndicatorSellInput1_1 = $_POST['StopIndicatorSellInput1_1'];
	$StopIndicatorSellInput1_2 = $_POST['StopIndicatorSellInput1_2'];
	$StopIndicatorSellInput1_3 = $_POST['StopIndicatorSellInput1_3'];	
	
	//Long Target indicator
	$LongIndicatorSell1_1 = $_POST['LongIndicatorSell1_1'];
	$LongIndicatorSell1_2 = $_POST['LongIndicatorSell1_2'];
	$LongIndicatorSell1_3 = $_POST['LongIndicatorSell1_3'];
	$LongIndicatorSellInput1_1 = $_POST['LongIndicatorSellInput1_1'];
	$LongIndicatorSellInput1_2 = $_POST['LongIndicatorSellInput1_2'];
	$LongIndicatorSellInput1_3 = $_POST['LongIndicatorSellInput1_3'];	
		
	


    $indexer = 1;//used to iterate through the technical analysis arrays

    //After some testing I found that in some instances buy and sell transactions where being made on the same day to stop this I am using the 
    //$Transactiondate variable to ensure that transactions cannot be made on the same date.
    $Transactiondate = "";

    //Used at the end of the script when calcualting the total profit/loss of the backtesting process, as if the type is buy then we have to use the variable
    //$initialCostOfShares where as if it is sell we have to use $StartingCapital;
    $lastTransactionType;


    //setup varibles for backtesting
    $canBuyShares = true;
    $initialCostOfShares = 0;
    $numberOfShares = 0;
	$WinningTrades=0;
	$LosingTrades=0;
	$TotalCommission=0;
	

	
	//Calculate the technical anaylysis arrays using the pecl trader php extension
	if($Buy1_1=="SMA")
   		$SMAArrayBuy1_1 = SMA($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
	if($Buy1_3=="SMA")
    	$SMAArrayBuy1_3 = SMA($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="SMA")
		$SMAArraySell_1 = SMA($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="SMA")
		$SMAArraySell1_3 = SMA($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="SMA")
		$SMAArraySell_1 = SMA($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_1=="SMA")
		$SMAArraySell1_3 = SMA($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);	
		
    if($LongIndicatorSell1_1=="SMA")
		$SMAArraySell_1 = SMA($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_1=="SMA")
		$SMAArraySell1_3 = SMA($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);		
	
	if($Buy1_1=="EMA")
		$EMAArrayBuy1_1 = EMA($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="EMA")
		$EMAArrayBuy1_3 = EMA($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="EMA")
		$EMAArraySell_1 = EMA($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="EMA")
		$EMAArraySell1_3 = EMA($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);

    if($StopIndicatorSell1_1=="EMA")
		$EMAArraySell_1 = EMA($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="EMA")
		$EMAArraySell1_3 = EMA($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);		

    if($LongIndicatorSell1_1=="EMA")
		$EMAArraySell_1 = EMA($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="EMA")
		$EMAArraySell1_3 = EMA($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);

	if($Buy1_1=="RSI")
		$RSIArrayBuy1_1 = RSI($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
		
    if($Buy1_3=="RSI")
		$RSIArrayBuy1_3 = RSI($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="RSI")
		$RSIArraySell_1 = RSI($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="RSI")
		$RSIArraySell1_3 = RSI($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
	if($StopIndicatorSell1_1=="RSI")
		$RSIArraySell_1 = RSI($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="RSI")
		$RSIArraySell1_3 = RSI($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);

	if($LongIndicatorSell1_1=="RSI")
		$RSIArraySell_1 = RSI($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="RSI")
		$RSIArraySell1_3 = RSI($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);
	
	if($Buy1_1=="SAR")
		$SARArrayBuy1_1 = SAR($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="SAR")
		$SARArrayBuy1_3 = SAR($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="SAR")
		$SARArraySell_1 = SAR($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="SAR")
   		$SARArraySell1_3 = SAR($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
	if($StopIndicatorSell1_1=="SAR")
		$SARArraySell_1 = SAR($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="SAR")
   		$SARArraySell1_3 = SAR($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
	
	if($LongIndicatorSell1_1=="SAR")
		$SARArraySell_1 = SAR($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="SAR")
   		$SARArraySell1_3 = SAR($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);

	if($Buy1_1=="WMA")
		$WMAArrayBuy1_1 = WMA($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="WMA")
		$WMAArrayBuy1_3 = WMA($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="WMA")
		$WMAArraySell_1 = WMA($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="WMA")
   		$WMAArraySell1_3 = WMA($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);	
		
    if($StopIndicatorSell1_1=="WMA")
		$WMAArraySell_1 = WMA($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="WMA")
   		$WMAArraySell1_3 = WMA($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);	
		
    if($LongIndicatorSell1_1=="WMA")
		$WMAArraySell_1 = WMA($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="WMA")
   		$WMAArraySell1_3 = WMA($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);	
		
	if($Buy1_1=="BollingerUpper")
		$BollingerUpperArrayBuy1_1 = Bollinger($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);

    if($Buy1_3=="BollingerUpper")
		$BollingerUpperArrayBuy1_3 = Bollinger($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
		
    if($Sell1_1=="BollingerUpper")
		$BollingerUpperArraySell_1 = Bollinger($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
		
    if($Sell1_3=="BollingerUpper")
   		$BollingerUpperArraySell1_3 = Bollinger($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);	
		
	if($StopIndicatorSell1_1=="BollingerUpper")
		$BollingerUpperArraySell_1 = Bollinger($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="BollingerUpper")
   		$BollingerUpperArraySell1_3 = Bollinger($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);	
		
	if($LongIndicatorSell1_1=="BollingerUpper")
		$BollingerUpperArraySell_1 = Bollinger($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="BollingerUpper")
   		$BollingerUpperArraySell1_3 = Bollinger($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);	


	if($Buy1_1=="BollingerLower")
		$BollingerLowerArrayBuy1_1 = Bollinger($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="BollingerLower")
		$BollingerLowerArrayBuy1_3 = Bollinger($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="BollingerLower")
		$BollingerLowerArraySell_1 = Bollinger($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="BollingerLower")
   		$BollingerLowerArraySell1_3 = Bollinger($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);	
		
	if($StopIndicatorSell1_1=="BollingerLower")
		$BollingerLowerArraySell_1 = Bollinger($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="BollingerLower")
   		$BollingerLowerArraySell1_3 = Bollinger($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);	
		
	if($LongIndicatorSell1_1=="BollingerLower")
		$BollingerLowerArraySell_1 = Bollinger($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="BollingerLower")
   		$BollingerLowerArraySell1_3 = Bollinger($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);	
	
	if($Buy1_1=="MacD")
		$MacDArrayBuy1_1 = MacD($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="MacD")
		$MacDArrayBuy1_3 = MacD($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="MacD"||$StopIndicatorSell1_1=="MacD")
		$MacDArraySell_1 = MacD($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="MacD"||$StopIndicatorSell1_3=="MacD")
   		$MacDArraySell1_3 = MacD($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
	if($StopIndicatorSell1_1=="MacD")
		$MacDArraySell_1 = MacD($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="MacD")
   		$MacDArraySell1_3 = MacD($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
	if($LongIndicatorSell1_1=="MacD")
		$MacDArraySell_1 = MacD($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="MacD")
   		$MacDArraySell1_3 = MacD($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);

	if($Buy1_1=="MacDSignal")
		$MacDSignalArrayBuy1_1 = MacD($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="MacDSignal")
		$MacDSignalArrayBuy1_3 = MacD($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="MacDSignal"||$StopIndicatorSell1_1=="MacDSignal")
		$MacDSignalArraySell_1 = MacD($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="MacDSignal"||$StopIndicatorSell1_3=="MacDSignal")
   		$MacDSignalArraySell1_3 = MacD($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
	if($StopIndicatorSell1_1=="MacDSignal")
		$MacDSignalArraySell_1 = MacD($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="MacDSignal")
   		$MacDSignalArraySell1_3 = MacD($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
	if($LongIndicatorSell1_1=="MacDSignal")
		$MacDSignalArraySell_1 = MacD($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="MacDSignal")
   		$MacDSignalArraySell1_3 = MacD($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);
		
	if($Buy1_1=="ROC")
		$ROCArrayBuy1_1 = ROC($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="ROC")
		$ROCArrayBuy1_3 = ROC($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="ROC"||$StopIndicatorSell1_1=="ROC")
		$ROCArraySell_1 = ROC($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="ROC"||$StopIndicatorSell1_3=="ROC")
   		$ROCArraySell1_3 = ROC($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="ROC")
		$ROCArraySell_1 = ROC($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="ROC")
   		$ROCArraySell1_3 = ROC($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
    if($LongIndicatorSell1_1=="ROC")
		$ROCArraySell_1 = ROC($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="ROC")
   		$ROCArraySell1_3 = ROC($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);
		
	if($Buy1_1=="OBV")
		$OBVArrayBuy1_1 = OBV($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="OBV")
		$OBVArrayBuy1_3 = OBV($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="OBV"||$StopIndicatorSell1_1=="OBV")
		$OBVArraySell_1 = OBV($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="OBV"||$StopIndicatorSell1_3=="OBV")
   		$OBVArraySell1_3 = OBV($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="OBV")
		$OBVArraySell_1 = OBV($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="OBV")
   		$OBVArraySell1_3 = OBV($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
    if($LongIndicatorSell1_1=="OBV")
		$OBVArraySell_1 = OBV($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="OBV")
   		$OBVArraySell1_3 = OBV($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);
		
	if($Buy1_1=="PVT")
		$PVTArrayBuy1_1 = PVT($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="PVT")
		$PVTArrayBuy1_3 = PVT($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="PVT"||$StopIndicatorSell1_1=="PVT")
		$PVTArraySell_1 = PVT($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="PVT"||$StopIndicatorSell1_3=="PVT")
   		$PVTArraySell1_3 = PVT($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="PVT")
		$PVTArraySell_1 = PVT($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="PVT")
   		$PVTArraySell1_3 = PVT($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
    if($LongIndicatorSell1_1=="PVT")
		$PVTArraySell_1 = PVT($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="PVT")
   		$PVTArraySell1_3 = PVT($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);
		
	if($Buy1_1=="WilliamR")
		$WilliamRArrayBuy1_1 = WilliamR($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="WilliamR")
		$WilliamRArrayBuy1_3 = WilliamR($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="WilliamR"||$StopIndicatorSell1_1=="WilliamR")
		$WilliamRArraySell_1 = WilliamR($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="WilliamR"||$StopIndicatorSell1_3=="WilliamR")
   		$WilliamRArraySell1_3 = WilliamR($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="WilliamR")
		$WilliamRArraySell_1 = WilliamR($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="WilliamR")
   		$WilliamRArraySell1_3 = WilliamR($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
	    if($LongIndicatorSell1_1=="WilliamR")
		$WilliamRArraySell_1 = WilliamR($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="WilliamR")
   		$WilliamRArraySell1_3 = WilliamR($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);

	if($Buy1_1=="MTM")
		$MTMArrayBuy1_1 = MTM($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="MTM")
		$MTMArrayBuy1_3 = MTM($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="MTM"||$StopIndicatorSell1_1=="MTM")
		$MTMArraySell_1 = MTM($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="MTM"||$StopIndicatorSell1_3=="MTM")
   		$MTMArraySell1_3 = MTM($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="MTM")
		$MTMArraySell_1 = MTM($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="MTM")
   		$MTMArraySell1_3 = MTM($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
    if($LongIndicatorSell1_1=="MTM")
		$MTMArraySell_1 = MTM($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="MTM")
   		$MTMArraySell1_3 = MTM($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);
		
	if($Buy1_1=="fastSTCk")
		$fastSTCkArrayBuy1_1 = FastSTC($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="fastSTCk")
		$fastSTCkArrayBuy1_3 = FastSTC($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="fastSTCk"||$StopIndicatorSell1_1=="fastSTCk")
		$fastSTCkArraySell_1 = FastSTC($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="fastSTCk"||$StopIndicatorSell1_3=="fastSTCk")
   		$fastSTCkArraySell1_3 = FastSTC($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="fastSTCk")
		$fastSTCkArraySell_1 = FastSTC($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="fastSTCk")
   		$fastSTCkArraySell1_3 = FastSTC($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
    if($LongIndicatorSell1_1=="fastSTCk")
		$fastSTCkArraySell_1 = FastSTC($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="fastSTCk")
   		$fastSTCkArraySell1_3 = FastSTC($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);

	if($Buy1_1=="fastSTCd")
		$fastSTCdArrayBuy1_1 = FastSTC($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="fastSTCd")
		$fastSTCdArrayBuy1_3 = FastSTC($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="fastSTCd"||$StopIndicatorSell1_1=="fastSTCd")
		$fastSTCdArraySell_1 = FastSTC($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="fastSTCd"||$StopIndicatorSell1_3=="fastSTCd")
   		$fastSTCdArraySell1_3 = FastSTC($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="fastSTCd")
		$fastSTCdArraySell_1 = FastSTC($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="fastSTCd")
   		$fastSTCdArraySell1_3 = FastSTC($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
    if($LongIndicatorSell1_1=="fastSTCd")
		$fastSTCdArraySell_1 = FastSTC($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="fastSTCd")
   		$fastSTCdArraySell1_3 = FastSTC($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);
		
	if($Buy1_1=="DMIplus")
		$DMIplusArrayBuy1_1 = DMI($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
		
    if($Buy1_3=="DMIplus")
		$DMIplusArrayBuy1_3 = DMI($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="DMIplus"||$StopIndicatorSell1_1=="DMIplus")
		$DMIplusArraySell_1 = DMI($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="DMIplus"||$StopIndicatorSell1_3=="DMIplus")
   		$DMIplusArraySell1_3 = DMI($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="DMIplus")
		$DMIplusArraySell_1 = DMI($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="DMIplus")
   		$DMIplusArraySell1_3 = DMI($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
    if($LongIndicatorSell1_1=="DMIplus")
		$DMIplusArraySell_1 = DMI($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="DMIplus")
   		$DMIplusArraySell1_3 = DMI($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);
	
	if($Buy1_1=="DMIminus")
		$DMIminusArrayBuy1_1 = DMI($StartDate, $EndDate, $BuyInput1_1, $Symbol,$con);
    if($Buy1_3=="DMIminus")
		$DMIminusArrayBuy1_3 = DMI($StartDate, $EndDate, $BuyInput1_3, $Symbol,$con);
    if($Sell1_1=="DMIminus"||$StopIndicatorSell1_1=="DMIminus")
		$DMIminusArraySell_1 = DMI($StartDate, $EndDate, $SellInput1_1, $Symbol,$con);
    if($Sell1_3=="DMIminus"||$StopIndicatorSell1_3=="DMIminus")
   		$DMIminusArraySell1_3 = DMI($StartDate, $EndDate, $SellInput1_3, $Symbol,$con);
		
    if($StopIndicatorSell1_1=="DMIminus")
		$DMIminusArraySell_1 = DMI($StartDate, $EndDate, $StopIndicatorSellInput1_1, $Symbol,$con);
    if($StopIndicatorSell1_3=="DMIminus")
   		$DMIminusArraySell1_3 = DMI($StartDate, $EndDate, $StopIndicatorSellInput1_3, $Symbol,$con);
		
    if($LongIndicatorSell1_1=="DMIminus")
		$DMIminusArraySell_1 = DMI($StartDate, $EndDate, $LongIndicatorSellInput1_1, $Symbol,$con);
    if($LongIndicatorSell1_3=="DMIminus")
   		$DMIminusArraySell1_3 = DMI($StartDate, $EndDate, $LongIndicatorSellInput1_3, $Symbol,$con);


    //obtain historical data in the range of the start date and end date from the database
    $historicalData = mysqli_query($con, "SELECT * FROM `shares` WHERE  `Symbol` =  '$Symbol' AND  `date` BETWEEN  '$StartDate' AND  '$EndDate'");

    //make a table to display the data in:	
    echo "<table border='1' cellspacing='5' cellpadding='2'>";
    echo"<tr>";
    echo"<td><strong>Date</strong></td>";
    echo"<td><strong>Signal</strong></td>";
    echo"<td><strong>Stock</strong></td>";
    echo"<td><strong>Price</strong></td>";
    echo"<td><strong>Number</strong></td>";
    echo"<td><strong>Value</strong></td>";

    echo"<td><strong>Profit/Loss</strong></td>";
    echo"<tr>";

    while ($row = mysqli_fetch_array($historicalData)) {

        $currentPriceOfShares = $row['Close'];
        $thedate = $row['Date'];

        //check if buy signal is met:
        if ($canBuyShares == true && $Transactiondate != $thedate) {
			
			//check $Buy1_1
            switch ($Buy1_1) {
                case "SMA":
                    $Buy1 = $SMAArrayBuy1_1[$indexer-1];
                    break;
                case "EMA":
                    $Buy1 = $EMAArrayBuy1_1[$indexer-1];
					break;
                case "ClosingPrice":
                    $Buy1 = $row['Close'];
                    break;
                case "OpeningPrice":
                    $Buy1 = $row['Open'];
                    break;
                case "Highest":
                    $Buy1 = $row['High'];
                    break;
                case "Lowest":
                    $Buy1 = $row['Low'];
                    break;
                case "Volume":
                    $Buy1 = $row['Volume'];
                    break;
				case "RSI":
					$Buy1= $RSIArrayBuy1_1[$indexer-1];
					break;
				case "Value":
					$Buy1= $BuyInput1_1;
					break;
				case "SAR":
					$Buy1= $SARArrayBuy1_1[0][$indexer];
					break;
				case "WMA":
					$Buy1= $WMAArrayBuy1_1[$indexer-1];
					break;
				case "BollingerUpper":
					$Buy1= $BollingerUpperArrayBuy1_1[0][$indexer-1];
					break;
				case "BollingerLower":
					$Buy1= $BollingerLowerArrayBuy1_1[2][$indexer-1];
					break;
				case "MacD":
					$Buy1= $MacDArrayBuy1_1[0][$indexer-1];
					break;
				case "MacDSignal":
					$Buy1= $MacDSignalArrayBuy1_1[1][$indexer-1];
					break;
				case "ROC":
					$Buy1= $ROCArrayBuy1_1[$indexer-1];
					break;
				case "OBV":
					$Buy1= $OBVArrayBuy1_1[0][$indexer-1];
					break;
				case "WilliamR":
					$Buy1= $WilliamRArrayBuy1_1[$indexer-1];
					break;
				case "MTM":
					$Buy1= $MTMArrayBuy1_1[$indexer-1];
					break;
				case "fastSTCk":
					$Buy1= $fastSTCkArrayBuy1_1[0][$indexer-1];
					break;
				case "fastSTCd":
					$Buy1= $fastSTCdArrayBuy1_1[1][$indexer-1];
					break;
				case "DMIplus":
					$Buy1= $DMIplusArrayBuy1_1[0][$indexer-1];
					break;
				case "DMIminus":
					$Buy1= $DMIminusArrayBuy1_1[1][$indexer-1];
					break;
				case "PVT":
					$Buy1= $PVTArrayBuy1_1[$indexer-1];
					break;
					
            }
            //check $Buy1_3
            switch ($Buy1_3) {
                case "SMA":
                    $Buy3 = $SMAArrayBuy1_3[$indexer-1];
                    break;
                case "EMA":
                    $Buy3 = $EMAArrayBuy1_3[$indexer-1];
					 break;
                case "ClosingPrice":
                    $Buy3 = $row['Close'];
                    break;
                case "OpeningPrice":
                    $Buy3 = $row['Open'];
                    break;
                case "Highest":
                    $Buy3 = $row['High'];
                    break;
                case "Lowest":
                    $Buy3 = $row['Low'];
                    break;
                case "Volume":
                    $Buy3 = $row['Volume'];
                    break;
				case "RSI":
					$Buy3= $RSIArrayBuy1_3[$indexer-1];
					break;
				case "Value":
					$Buy3= $BuyInput1_3;
					break;
				case "SAR":
					$Buy3= $SARArrayBuy1_3[0][$indexer];
					break;
				case "WMA":
					$Buy3= $WMAArrayBuy1_3[$indexer-1];
					break;
				case "BollingerUpper":
					$Buy3= $BollingerUpperArrayBuy1_3[0][$indexer-1];
					break;
				case "BollingerLower":
					$Buy3= $BollingerLowerArrayBuy1_3[2][$indexer-1];
					break;
				case "MacD":
					$Buy3= $MacDArrayBuy1_3[0][$indexer-1];
					break;
				case "MacDSignal":
					$Buy3= $MacDSignalArrayBuy1_3[1][$indexer-1];
					break;
				case "ROC":
					$Buy3= $ROCArrayBuy1_3[$indexer-1];
					break;
				case "OBV":
					$Buy3= $OBVArrayBuy1_3[0][$indexer-1];
					break;
				case "WilliamR":
					$Buy3= $WilliamRArrayBuy1_3[$indexer-1];
					break;
				case "MTM":
					$Buy3= $MTMArrayBuy1_3[$indexer-1];
					break;
				case "fastSTCk":
					$Buy3= $fastSTCkArrayBuy1_3[0][$indexer-1];
					break;
				case "fastSTCd":
					$Buy3= $fastSTCdArrayBuy1_3[1][$indexer-1];
					break;
				case "DMIplus":
					$Buy3= $DMIplusArrayBuy1_3[0][$indexer-1];
					break;
				case "DMIminus":
					$Buy3= $DMIminusArrayBuy1_3[1][$indexer-1];
					break;
				case "PVT":
					$Buy3= $PVTArrayBuy1_3[$indexer-1];
					break;
					
					
            }
            //check if the buy signal is a match:
            switch ($Buy1_2) {
                case "Above":
                    //Buy1_1>Buy1_3
                    if ($Buy1 > $Buy3) {
						$numberOfShares=0;
                        //buy as many shares as you can
                        $StartingCapital = $StartingCapital - $TradingCommission;
                        for ($i = $StartingCapital - $TradingCommission; $i > 0; $i-=$currentPriceOfShares) {
                            $numberOfShares+=1;
                        }
						
						$initialCostOfShares = $numberOfShares * $currentPriceOfShares + $TradingCommission;
						 //double check that the cost is not more than starting capital
                        if ($StartingCapital - $TradingCommission < $initialCostOfShares) {
                            $initialCostOfShares-=$currentPriceOfShares;
								//$numberOfShares-=1;
							
                        }
						//check if the lock size is met
						if($numberOfShares>=$LockSize){
                        //calculate the cost
                        $initialCostOfShares = $numberOfShares * $currentPriceOfShares + $TradingCommission;

                       
                        //remove cost of shares from starting capital
                        $StartingCapital-=$initialCostOfShares;
                        $canBuyShares = false;

                        //stop multiple transactions on the same day:
                        $Transactiondate = $thedate;
                        $lastTransactionType = "Buy";
                        //output here

                        TableOutput($thedate, "Buy", $Symbol, $currentPriceOfShares, $numberOfShares, $initialCostOfShares, 0);
						
						//add all the things to the arrays to send to the javascript graphs
						$Value= $currentPriceOfShares*$numberOfShares;
						array_push($gainArray,0);
						array_push($profitLossArray,$Value);
						array_push($numberOfSharesArray,$numberOfShares);	
						array_push($dates,$thedate);
						array_push($signalsArray,"Buy");
						}
						
                    }
                    break;
                case "Below":
                    //Buy1_1<Buy1_3

                    if ($Buy1 < $Buy3) {
						$numberOfShares=0;
                        //buy as many shares as you can
                        for ($i = $StartingCapital - $TradingCommission; $i >= 0; $i-=$currentPriceOfShares) {
                            $numberOfShares+=1;
                        }
						 $initialCostOfShares = $numberOfShares * $currentPriceOfShares + $TradingCommission;
						if ($StartingCapital - $TradingCommission < $initialCostOfShares) {
                            $initialCostOfShares-=$currentPriceOfShares;
							 $numberOfShares-=1;
                        }
						//check if the lock size is met
						if($numberOfShares>=$LockSize){
                        //calculate the cost
                       

                        //double check that the cost is not more than starting capital
                        
                        //remove cost of shares from starting capital
                        $StartingCapital-=$initialCostOfShares;
                        $canBuyShares = false;
                        //stop multiple transactions on the same day:
                        $Transactiondate = $thedate;
                        $lastTransactionType = "Buy";

                        //output here

                        TableOutput($thedate, "Buy", $Symbol, $currentPriceOfShares, $numberOfShares, $initialCostOfShares, 0);
						//add all the things to the arrays to send to the javascript graphs
						$Value= $currentPriceOfShares*$numberOfShares;
						array_push($gainArray,0);
						array_push($profitLossArray,$Value);
						array_push($numberOfSharesArray,$numberOfShares);	
						array_push($dates,$thedate);
						array_push($signalsArray,"Buy");
                    }
					}
                    break;
                case "AtLeast%Above%":

                    $percentage = CalculatePercentage($Buy1, $Buy3);

					
                    if ($percentage > $BuyInput1_2) {
						$numberOfShares=0;
                        for ($i = $StartingCapital - $TradingCommission; $i >= 0; $i-=$currentPriceOfShares) {
                            $numberOfShares+=1;
                        }
						
						$initialCostOfShares = $numberOfShares * $currentPriceOfShares + $TradingCommission;
						//double check that the cost is not more than starting capital
                        if ($StartingCapital - $TradingCommission < $initialCostOfShares) {
                            $initialCostOfShares-=$currentPriceOfShares;
							$numberOfShares-=1;
                        }
						
						//check if the lock size is met
						if($numberOfShares>=$LockSize){
                        //calculate the cost
                        
                        //remove cost of shares from starting capital
                        $StartingCapital-=$initialCostOfShares;
                        $canBuyShares = false;
                        //stop multiple transactions on the same day:
                        $Transactiondate = $thedate;
                        $lastTransactionType = "Buy";

                        TableOutput($thedate, "Buy", $Symbol, $currentPriceOfShares, $numberOfShares, $initialCostOfShares, 0);
						//add all the things to the arrays to send to the javascript graphs
						$Value= $currentPriceOfShares*$numberOfShares;
						array_push($gainArray,0);
						array_push($profitLossArray,$Value);
						array_push($numberOfSharesArray,$numberOfShares);	
						array_push($dates,$thedate);
						array_push($signalsArray,"Buy");
                    }
					}
                    break;
                case "AtLeast%Below%":

                    $percentage = CalculatePercentage($Buy1, $Buy3);

                    if ($percentage < -$BuyInput1_2) {
						$numberOfShares=0;
                        for ($i = ($StartingCapital - $TradingCommission); $i >= 0; $i-=$currentPriceOfShares) {
                            $numberOfShares+=1;
                        }
						 $initialCostOfShares = $numberOfShares * $currentPriceOfShares + $TradingCommission;
						  //double check that the cost is not more than starting capital
                        if ($StartingCapital - $TradingCommission < $initialCostOfShares) {
                            $initialCostOfShares-=$currentPriceOfShares;
							$numberOfShares-=1;
                        }
						//check if the lock size is met
						if($numberOfShares>=$LockSize){
							
                        //calculate the cost
                        //remove cost of shares from starting capital
                        $StartingCapital-=$initialCostOfShares;
                        $canBuyShares = false;
                        //stop multiple transactions on the same day:
                        $Transactiondate = $thedate;
                        $lastTransactionType = "Buy";
                        TableOutput($thedate, "Buy", $Symbol, $currentPriceOfShares, $numberOfShares, $initialCostOfShares, 0);
						//add all the things to the arrays to send to the javascript graphs
						$Value= $currentPriceOfShares*$numberOfShares;
						array_push($gainArray,0);
						array_push($profitLossArray,$Value);
						array_push($numberOfSharesArray,$numberOfShares);	
						array_push($dates,$thedate);
						array_push($signalsArray,"Buy");
                    }
					}
                    break;
            }
        }

        //check if we can sell
        if ($canBuyShares == false && $Transactiondate != $thedate) {
            
            switch ($Sell1_1) {
                case "SMA":
                    $Sell1 = $SMAArraySell_1[$indexer-1];
                    break;
                case "EMA":
                    $Sell1 = $EMAArraySell_1 [$indexer-1];
                    break;
                case "ClosingPrice":
                    $Sell1 = $row['Close'];
                    break;
                case "OpeningPrice":
                    $Sell1 = $row['Open'];
                    break;
                case "Highest":
                    $Sell1 = $row['High'];
                    break;
                case "Lowest":
                    $Sell1 = $row['Low'];
                    break;
                case "Volume":
                    $Sell1 = $row['Volume'];
                    break;
				case "RSI":
					$Sell1 = $RSIArraySell_1[$indexer-1];
					break;
				case "Value":
					$Sell1 = $SellInput1_1;
					break;
				case "SAR":
					$Sell1 = $SARArraySell_1[0][$indexer];
					break;
				case "WMA":
					$Sell1 = $WMAArraySell_1[$indexer-1];
					break;
				case "BollingerUpper":
					$Sell1 = $BollingerUpperArraySell_1[0][$indexer-1];
					break;
				case "BollingerLower":
					$Sell1 = $BollingerLowerArraySell_1[2][$indexer-1];
					break;
				case "MacD":
					$Sell1 = $MacDArraySell_1[0][$indexer-1];
					break;
				case "MacDSignal":
					$Sell1 = $MacDSignalArraySell_1[1][$indexer-1];
					break;
				case "ROC":
					$Sell1 = $ROCArraySell_1[$indexer-1];
					break;
				case "OBV":
					$Sell1 = $OBVArraySell_1[0][$indexer-1];
					break;
				case "WilliamR":
					$Sell1 = $WilliamRArraySell_1[$indexer-1];
					break;
				case "MTM":
					$Sell1 = $MTMArraySell_1[$indexer-1];
					break;
				case "fastSTCk":
					$Sell1 = $fastSTCkArraySell_1[0][$indexer-1];
					break;
				case "fastSTCd":
					$Sell1 = $fastSTCdArraySell_1[1][$indexer-1];
					break;
				case "DMIplus":
					$Sell1 = $DMIplusArraySell_1[0][$indexer-1];
					break;
				case "DMIminus":
					$Sell1 = $DMIminusArraySell_1[1][$indexer-1];
					break;
				case "PVT":
					$Sell1 = $PVTArraySell_1[$indexer-1];
					break;
			}
				//STOP Long Indicator 	
				if($StopLong=="StopIndicator"){
					
					switch ($StopIndicatorSell1_1) {
						case "SMA":
							$StopIndicatorSell1 = $SMAArraySell_1[$indexer-1];
							break;
						case "EMA":
							$StopIndicatorSell1 = $EMAArraySell_1 [$indexer-1];
							break;
						case "ClosingPrice":
							$StopIndicatorSell1 = $row['Close'];
							break;
						case "OpeningPrice":
							$StopIndicatorSell1 = $row['Open'];
							break;
						case "Highest":
							$StopIndicatorSell1 = $row['High'];
							break;
						case "Lowest":
							$StopIndicatorSell1 = $row['Low'];
							break;
						case "Volume":
							$StopIndicatorSell1 = $row['Volume'];
							break;
						case "RSI":
							$StopIndicatorSell1 = $RSIArraySell_1[$indexer-1];
							break;
						case "Value":
							$StopIndicatorSell1 = $SellInput1_1;
							break;
						case "SAR":
							$StopIndicatorSell1 = $SARArraySell_1[0][$indexer];
							break;
						case "WMA":
							$StopIndicatorSell1 = $WMAArraySell_1[$indexer-1];
							break;
						case "BollingerUpper":
							$StopIndicatorSell1 = $BollingerUpperArraySell_1[0][$indexer-1];
							break;
						case "BollingerLower":
							$StopIndicatorSell1 = $BollingerLowerArraySell_1[2][$indexer-1];
							break;
						case "MacD":
							$StopIndicatorSell1 = $MacDArraySell_1[0][$indexer-1];
							break;
						case "MacDSignal":
							$StopIndicatorSell1 = $MacDSignalArraySell_1[1][$indexer-1];
							break;
						case "ROC":
							$StopIndicatorSell1 = $ROCArraySell_1[$indexer-1];
							break;
						case "OBV":
							$StopIndicatorSell1 = $OBVArraySell_1[0][$indexer-1];
							break;
						case "WilliamR":
							$StopIndicatorSell1 = $WilliamRArraySell_1[$indexer-1];
							break;
						case "MTM":
							$StopIndicatorSell1 = $MTMArraySell_1[$indexer-1];
							break;
						case "fastSTCk":
							$StopIndicatorSell1 = $fastSTCkArraySell_1[0][$indexer-1];
							break;
						case "fastSTCd":
							$StopIndicatorSell1 = $fastSTCdArraySell_1[1][$indexer-1];
							break;
						case "DMIplus":
							$StopIndicatorSell1 = $DMIplusArraySell_1[0][$indexer-1];
							break;
						case "DMIminus":
							$StopIndicatorSell1 = $DMIminusArraySell_1[1][$indexer-1];
							break;
						case "PVT":
							$StopIndicatorSell1 = $PVTArraySell_1[$indexer-1];
							break;

					}
				
            }
			
				//Long TARGET Indicator 	
				if($LongTarget=="LongIndicator"){
					
					switch ($LongIndicatorSell1_1) {
						case "SMA":
							$LongIndicatorSell1 = $SMAArraySell_1[$indexer-1];
							break;
						case "EMA":
							$LongIndicatorSell1 = $EMAArraySell_1 [$indexer-1];
							break;
						case "ClosingPrice":
							$LongIndicatorSell1 = $row['Close'];
							break;
						case "OpeningPrice":
							$LongIndicatorSell1 = $row['Open'];
							break;
						case "Highest":
							$LongIndicatorSell1 = $row['High'];
							break;
						case "Lowest":
							$LongIndicatorSell1 = $row['Low'];
							break;
						case "Volume":
							$LongIndicatorSell1 = $row['Volume'];
							break;
						case "RSI":
							$LongIndicatorSell1 = $RSIArraySell_1[$indexer-1];
							break;
						case "Value":
							$LongIndicatorSell1 = $SellInput1_1;
							break;
						case "SAR":
							$LongIndicatorSell1 = $SARArraySell_1[0][$indexer];
							break;
						case "WMA":
							$LongIndicatorSell1 = $WMAArraySell_1[$indexer-1];
							break;
						case "BollingerUpper":
							$LongIndicatorSell1 = $BollingerUpperArraySell_1[0][$indexer-1];
							break;
						case "BollingerLower":
							$LongIndicatorSell1 = $BollingerLowerArraySell_1[2][$indexer-1];
							break;
						case "MacD":
							$LongIndicatorSell1 = $MacDArraySell_1[0][$indexer-1];
							break;
						case "MacDSignal":
							$LongIndicatorSell1 = $MacDSignalArraySell_1[1][$indexer-1];
							break;
						case "ROC":
							$LongIndicatorSell1 = $ROCArraySell_1[$indexer-1];
							break;
						case "OBV":
							$LongIndicatorSell1 = $OBVArraySell_1[0][$indexer-1];
							break;
						case "WilliamR":
							$LongIndicatorSell1 = $WilliamRArraySell_1[$indexer-1];
							break;
						case "MTM":
							$LongIndicatorSell1 = $MTMArraySell_1[$indexer-1];
							break;
						case "fastSTCk":
							$LongIndicatorSell1 = $fastSTCkArraySell_1[0][$indexer-1];
							break;
						case "fastSTCd":
							$LongIndicatorSell1 = $fastSTCdArraySell_1[1][$indexer-1];
							break;
						case "DMIplus":
							$LongIndicatorSell1 = $DMIplusArraySell_1[0][$indexer-1];
							break;
						case "DMIminus":
							$LongIndicatorSell1 = $DMIminusArraySell_1[1][$indexer-1];
							break;
						case "PVT":
							$LongIndicatorSell1 = $PVTArraySell_1[$indexer-1];
							break;
					}
				
            }			
			
			//check $Buy1_3
			switch ($Sell1_3) {
				case "SMA":
					$Sell3 = $SMAArraySell1_3[$indexer-1];
					break;
				case "EMA":
					$Sell3 = $EMAArraySell1_3 [$indexer-1];
					break;
				 case "ClosingPrice":
                    $Sell3 = $row['Close'];
                    break;
                case "OpeningPrice":
                    $Sell3 = $row['Open'];
                    break;
                case "Highest":
                    $Sell3 = $row['High'];
                    break;
                case "Lowest":
                    $Sell3 = $row['Low'];
                    break;
                case "Volume":
                    $Sell3 = $row['Volume'];
                    break;
				case "RSI":
					$Sell3 = $RSIArraySell1_3[$indexer-1];
					break;
				case "Value":
					$Sell3 = $SellInput1_3;
					break;
				case "SAR":
					$Sell3 = $SARArraySell1_3[0][$indexer];
					break;
				case "WMA":
					$Sell3 = $WMAArraySell1_3[$indexer-1];
					break;
				case "BollingerUpper":
					$Sell3 = $BollingerUpperArraySell1_3[0][$indexer-1];
					break;
				case "BollingerLower":
					$Sell3 = $BollingerLowerArraySell1_3[2][$indexer-1];
					break;
				case "MacD":
					$Sell3 = $MacDArraySell1_3[0][$indexer-1];
					break;
				case "MacDSignal":
					$Sell3 = $MacDSignalArraySell1_3[1][$indexer-1];
					break;
				case "ROC":
					$Sell3 = $ROCArraySell1_3[$indexer-1];
					break;
				case "OBV":
					$Sell3 = $OBVArraySell1_3[0][$indexer-1];
					break;
				case "WilliamR":
					$Sell3 = $WilliamRArraySell1_3[$indexer-1];
					break;
				case "MTM":
					$Sell3 = $MTMArraySell1_3[$indexer-1];
					break;
				case "fastSTCk":
					$Sell3 = $fastSTCkArraySell1_3[0][$indexer-1];
					break;
				case "fastSTCd":
					$Sell3 = $fastSTCdArraySell1_3[1][$indexer-1];
					break;
				case "DMIplus":
					$Sell3 = $DMIplusArraySell1_3[0][$indexer-1];
					break;
				case "DMIminus":
					$Sell3 = $DMIminusArraySell1_3[1][$indexer-1];
					break;
				case "PVT":
					$Sell3 = $PVTArraySell1_3[$indexer-1];
					break;
					
	
			}
			//STOP Long Indicator 	
			if($StopLong=="StopIndicator"){
					
				switch ($StopIndicatorSell1_3) {
				case "SMA":
					$StopIndicatorSell3 = $SMAArraySell1_3[$indexer-1];
					break;
				case "EMA":
					$StopIndicatorSell3 = $EMAArraySell1_3 [$indexer-1];
					break;
				 case "ClosingPrice":
                    $StopIndicatorSell3 = $row['Close'];
                    break;
                case "OpeningPrice":
                    $StopIndicatorSell3 = $row['Open'];
                    break;
                case "Highest":
                    $StopIndicatorSell3 = $row['High'];
                    break;
                case "Lowest":
                    $StopIndicatorSell3 = $row['Low'];
                    break;
                case "Volume":
                    $StopIndicatorSell3 = $row['Volume'];
                    break;
				case "RSI":
					$StopIndicatorSell3 = $RSIArraySell1_3[$indexer-1];
					break;
				case "Value":
					$StopIndicatorSell3 = $SellInput1_3;
					break;
				case "SAR":
					$StopIndicatorSell3 = $SARArraySell1_3[0][$indexer];
					break;
				case "WMA":
					$StopIndicatorSell3 = $WMAArraySell1_3[$indexer-1];
					break;
				case "BollingerUpper":
					$StopIndicatorSell3 = $BollingerUpperArraySell1_3[0][$indexer-1];
					break;
				case "BollingerLower":
					$StopIndicatorSell3 = $BollingerLowerArraySell1_3[2][$indexer-1];
					break;
				case "MacD":
					$StopIndicatorSell3 = $MacDArraySell1_3[0][$indexer-1];
					break;
				case "MacDSignal":
					$StopIndicatorSell3 = $MacDSignalArraySell1_3[1][$indexer-1];
					break;
				case "ROC":
					$StopIndicatorSell3 = $ROCArraySell1_3[$indexer-1];
					break;
				case "OBV":
					$StopIndicatorSell3 = $OBVArraySell1_3[0][$indexer-1];
					break;
				case "WilliamR":
					$StopIndicatorSell3 = $WilliamRArraySell1_3[$indexer-1];
					break;
				case "MTM":
					$StopIndicatorSell3 = $MTMArraySell1_3[$indexer-1];
					break;
				case "fastSTCk":
					$StopIndicatorSell3 = $fastSTCkArraySell1_3[0][$indexer-1];
					break;
				case "fastSTCd":
					$StopIndicatorSell3 = $fastSTCdArraySell1_3[1][$indexer-1];
					break;
				case "DMIplus":
					$StopIndicatorSell3 = $DMIplusArraySell1_3[0][$indexer-1];
					break;
				case "DMIminus":
					$StopIndicatorSell3 = $DMIminusArraySell1_3[1][$indexer-1];
					break;
				case "PVT":
					$StopIndicatorSell3 = $PVTArraySell1_3[$indexer-1];
					break;

				}
			}
			
			//Long Target Indicator 	
			if($LongTarget=="LongIndicator"){
					
				switch ($LongIndicatorSell1_3) {
				case "SMA":
					$LongIndicatorSell3 = $SMAArraySell1_3[$indexer-1];
					break;
				case "EMA":
					$LongIndicatorSell3 = $EMAArraySell1_3 [$indexer-1];
					break;
				 case "ClosingPrice":
                    $LongIndicatorSell3 = $row['Close'];
                    break;
                case "OpeningPrice":
                    $LongIndicatorSell3 = $row['Open'];
                    break;
                case "Highest":
                    $LongIndicatorSell3 = $row['High'];
                    break;
                case "Lowest":
                    $LongIndicatorSell3 = $row['Low'];
                    break;
                case "Volume":
                    $LongIndicatorSell3 = $row['Volume'];
                    break;
				case "RSI":
					$LongIndicatorSell3 = $RSIArraySell1_3[$indexer-1];
					break;
				case "Value":
					$LongIndicatorSell3 = $SellInput1_3;
					break;
				case "SAR":
					$LongIndicatorSell3 = $SARArraySell1_3[0][$indexer];
					break;
				case "WMA":
					$LongIndicatorSell3 = $WMAArraySell1_3[$indexer-1];
					break;
				case "BollingerUpper":
					$LongIndicatorSell3 = $BollingerUpperArraySell1_3[0][$indexer-1];
					break;
				case "BollingerLower":
					$LongIndicatorSell3 = $BollingerLowerArraySell1_3[2][$indexer-1];
					break;
				case "MacD":
					$LongIndicatorSell3 = $MacDArraySell1_3[0][$indexer-1];
					break;
				case "MacDSignal":
					$LongIndicatorSell3 = $MacDSignalArraySell1_3[1][$indexer-1];
					break;
				case "ROC":
					$LongIndicatorSell3 = $ROCArraySell1_3[$indexer-1];
					break;
				case "OBV":
					$LongIndicatorSell3 = $OBVArraySell1_3[0][$indexer-1];
					break;
				case "WilliamR":
					$LongIndicatorSell3 = $WilliamRArraySell1_3[$indexer-1];
					break;
				case "MTM":
					$LongIndicatorSell3 = $MTMArraySell1_3[$indexer-1];
					break;
				case "fastSTCk":
					$LongIndicatorSell3 = $fastSTCkArraySell1_3[0][$indexer-1];
					break;
				case "fastSTCd":
					$LongIndicatorSell3 = $fastSTCdArraySell1_3[1][$indexer-1];
					break;
				case "DMIplus":
					$LongIndicatorSell3 = $DMIplusArraySell1_3[0][$indexer-1];
					break;
				case "DMIminus":
					$LongIndicatorSell3 = $DMIminusArraySell1_3[1][$indexer-1];
					break;
				case "PVT":
					$LongIndicatorSell3 = $PVTArraySell1_3[$indexer-1];
					break;
					
	
				}
			}
            //Check if the sell signal algorithm is true:
            switch ($Sell1_2) {
                case "Above":
                    //Sell1_1>Sell1_3
                    if ($Sell1 > $Sell3) {
                        //Calculate value of shares
                        $Value = ($numberOfShares * $currentPriceOfShares);
                        //sell the shares
                        $StartingCapital+=($Value - $TradingCommission);
                        $profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;
						//check if the sell is a winning trade
							if(IsWinning($profitLoss)){
								$WinningTrades+=1;	
							}else{$LosingTrades+=1;}
						
                        //stop multiple transactions on the same day:
                        $Transactiondate = $thedate;
                        $lastTransactionType = "Sell";
                        TableOutput($thedate, "Sell", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
						//add all the things to the arrays to send to the javascript graphs
						$Value= $currentPriceOfShares*$numberOfShares;
						array_push($gainArray,$profitLoss);
						array_push($profitLossArray,$Value);
						array_push($numberOfSharesArray,$numberOfShares);	
						array_push($dates,$thedate);
						array_push($signalsArray,"Sell");
                        $numberOfShares = 0;
                        $canBuyShares = true;
                    }
                    break;
                case "Below":
                    //Sell1_1<Sell1_3

                    if ($Sell1 < $Sell3) {

                        //Calculate value of shares
                        $Value = ($numberOfShares * $currentPriceOfShares);
                        //sell the shares
                        $StartingCapital+=($Value - $TradingCommission);


                        $profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;

						
						//check if the sell is a winning trade
							if(IsWinning($profitLoss)){
								$WinningTrades+=1;	
							}else{$LosingTrades+=1;}		
									
                        //stop multiple transactions on the same day:
                        $Transactiondate = $thedate;
                        $lastTransactionType = "Sell";
                        TableOutput($thedate, "Sell", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
						//add all the things to the arrays to send to the javascript graphs
						$Value= $currentPriceOfShares*$numberOfShares;
						array_push($gainArray,$profitLoss);
						array_push($profitLossArray,$Value);
						array_push($numberOfSharesArray,$numberOfShares);	
						array_push($dates,$thedate);
						array_push($signalsArray,"Sell");
                        $numberOfShares = 0;
                        $canBuyShares = true;
                    }
                    break;
                case "AtLeast%Above%":
                    $percentage = CalculatePercentage($Sell1, $Sell3);


                    if ($percentage > $SellInput1_2) {

                        //Calculate value of shares
                        $Value = ($numberOfShares * $currentPriceOfShares);
                        //sell the shares

                        $StartingCapital+=($Value - $TradingCommission);

                        $profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;
	
						
						//check if the sell is a winning trade
							if(IsWinning($profitLoss)){
								$WinningTrades+=1;	
							}else{$LosingTrades+=1;}
						
                        //stop multiple transactions on the same day:
                        $Transactiondate = $thedate;
                        $lastTransactionType = "Sell";
                        TableOutput($thedate, "Sell", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
						//add all the things to the arrays to send to the javascript graphs
						$Value= $currentPriceOfShares*$numberOfShares;
						array_push($gainArray,$profitLoss);
						array_push($profitLossArray,$Value);
						array_push($numberOfSharesArray,$numberOfShares);	
						array_push($dates,$thedate);
						array_push($signalsArray,"Sell");
                        $numberOfShares = 0;
                        $canBuyShares = true;
                    }
                    break;
                case "AtLeast%Below%":
                    $percentage = CalculatePercentage($Sell1, $Sell3);


                    if ($percentage <-$SellInput1_2) {

                        //Calculate value of shares
                        $Value = ($numberOfShares * $currentPriceOfShares);
                        //sell the shares
                        $StartingCapital+=($Value - $TradingCommission);


                        $profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;
							
						//check if the sell is a winning trade
							if(IsWinning($profitLoss)){
								$WinningTrades+=1;	
							}else{$LosingTrades+=1;}
						
                        //stop multiple transactions on the same day:
                        $Transactiondate = $thedate;
                        $lastTransactionType = "Sell";

                        TableOutput($thedate, "Sell", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
						//add all the things to the arrays to send to the javascript graphs
						$Value= $currentPriceOfShares*$numberOfShares;
						array_push($gainArray,$profitLoss);
						array_push($profitLossArray,$Value);
						array_push($numberOfSharesArray,$numberOfShares);	
						array_push($dates,$thedate);
						array_push($signalsArray,"Sell");
                        $numberOfShares = 0;
                        $canBuyShares = true;
                    }
                    break;
            }
			

            //Risk acceptance level check
            if ($canBuyShares == false && $Transactiondate != $thedate) {
                //test risk level stop 
                switch ($StopLong) {
                    case "StopIndicator":
                        //STOP Long Indicator 	
						if($StopLong=="StopIndicator"){
							switch ($StopIndicatorSell1_2) {
								case "Above":
				
									//Sell1_1>Sell1_3
									if ($StopIndicatorSell1 > $StopIndicatorSell3) {
										//Calculate value of shares
										$Value = ($numberOfShares * $currentPriceOfShares);
										//sell the shares
										$StartingCapital+=($Value - $TradingCommission);
										$profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;

										
										//check if the sell is a winning trade
											if(IsWinning($profitLoss)){
												$WinningTrades+=1;	
											}else{$LosingTrades+=1;}
										
										//stop multiple transactions on the same day:
										$Transactiondate = $thedate;
										$lastTransactionType = "Stop Long";
										TableOutput($thedate, "Stop Long", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
										//add all the things to the arrays to send to the javascript graphs
										$Value= $currentPriceOfShares*$numberOfShares;
										array_push($gainArray,$profitLoss);
										array_push($profitLossArray,$Value);
										array_push($numberOfSharesArray,$numberOfShares);	
										array_push($dates,$thedate);
										array_push($signalsArray,"Stop Long");
										$numberOfShares = 0;
										$canBuyShares = true;
									}
									break;
								case "Below":
									//Sell1_1<Sell1_3
				
									if ($StopIndicatorSell1 < $StopIndicatorSell3) {
				
										//Calculate value of shares
										$Value = ($numberOfShares * $currentPriceOfShares);
										//sell the shares
										$StartingCapital+=($Value - $TradingCommission);
				
				
										$profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;

										//check if the sell is a winning trade
											if(IsWinning($profitLoss)){
												$WinningTrades+=1;	
											}else{$LosingTrades+=1;}		
													
										//stop multiple transactions on the same day:
										$Transactiondate = $thedate;
										$lastTransactionType = "Stop Long";
										TableOutput($thedate, "Stop Long", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
										//add all the things to the arrays to send to the javascript graphs
										$Value= $currentPriceOfShares*$numberOfShares;
										array_push($gainArray,$profitLoss);
										array_push($profitLossArray,$Value);
										array_push($numberOfSharesArray,$numberOfShares);	
										array_push($dates,$thedate);
										array_push($signalsArray,"Stop Long");
										$numberOfShares = 0;
										$canBuyShares = true;
									}
									break;
								case "AtLeast%Above%":
									$percentage = CalculatePercentage($StopIndicatorSell1, $StopIndicatorSell3);
									
			
									if ($percentage > $StopIndicatorSellInput1_2) {
				
										//Calculate value of shares
										$Value = ($numberOfShares * $currentPriceOfShares);
										//sell the shares
				
										$StartingCapital+=($Value - $TradingCommission);
				
										$profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;
										
										//check if the sell is a winning trade
											if(IsWinning($profitLoss)){
												$WinningTrades+=1;	
											}else{$LosingTrades+=1;}
										
										//stop multiple transactions on the same day:
										$Transactiondate = $thedate;
										$lastTransactionType = "Stop Long";
										TableOutput($thedate, "Stop Long", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
										//add all the things to the arrays to send to the javascript graphs
										$Value= $currentPriceOfShares*$numberOfShares;
										array_push($gainArray,$profitLoss);
										array_push($profitLossArray,$Value);
										array_push($numberOfSharesArray,$numberOfShares);	
										array_push($dates,$thedate);
										array_push($signalsArray,"Stop Long");
										$numberOfShares = 0;
										$canBuyShares = true;
									}
									break;
								case "AtLeast%Below%":
									$percentage = CalculatePercentage($StopIndicatorSell1, $StopIndicatorSell3);
									if ($percentage <-$StopIndicatorSellInput1_2) {
				
										//Calculate value of shares
										$Value = ($numberOfShares * $currentPriceOfShares);
										//sell the shares
										$StartingCapital+=($Value - $TradingCommission);
				
				
										$profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;
	
										//check if the sell is a winning trade
											if(IsWinning($profitLoss)){
												$WinningTrades+=1;	
											}else{$LosingTrades+=1;}
										
										//stop multiple transactions on the same day:
										$Transactiondate = $thedate;
										$lastTransactionType = "Stop Long";
				
										TableOutput($thedate, "Stop Long", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
										//add all the things to the arrays to send to the javascript graphs
										$Value= $currentPriceOfShares*$numberOfShares;
										array_push($gainArray,$profitLoss);
										array_push($profitLossArray,$Value);
										array_push($numberOfSharesArray,$numberOfShares);	
										array_push($dates,$thedate);
										array_push($signalsArray,"Stop Long");
										$numberOfShares = 0;
										$canBuyShares = true;
									}
									break;
							}				
							
						}
                        break;
                    case "Stop%":
                        //calculate the % profit
                        $profit = CalculateProfitPercentage($numberOfShares, $currentPriceOfShares, $TradingCommission, $initialCostOfShares);
                        //check if the profit is at a loss greater than the risk acceptance level.
                        if ($profit <= -$StopLongInput) {
                            //sell the shares
                            //Calculate value of shares
                            $Value = ($numberOfShares * $currentPriceOfShares);
                            //sell the shares
                            $StartingCapital+=($Value - $TradingCommission);

                            $profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;
						
							//check if the sell is a winning trade
							if(IsWinning($profitLoss)){
								$WinningTrades+=1;	
							}else{$LosingTrades+=1;}
							
                            //stop multiple transactions on the same day:
                            $Transactiondate = $thedate;
                            $lastTransactionType = "Sell";
                            TableOutput($thedate, "Stop Long", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
							//add all the things to the arrays to send to the javascript graphs
							$Value= $currentPriceOfShares*$numberOfShares;
							array_push($gainArray,$profitLoss);
							array_push($profitLossArray,$Value);
							array_push($numberOfSharesArray,$numberOfShares);	
							array_push($dates,$thedate);
							array_push($signalsArray,"Stop Long");
                            $numberOfShares = 0;
                            $canBuyShares = true;
                        }
                        break;
                }

                //test profit level stop
                switch ($LongTarget) {
                    case "UseReturnTarget%":
                        $profit = CalculateProfitPercentage($numberOfShares, $currentPriceOfShares, $TradingCommission, $initialCostOfShares);
                        if ($profit >= $LongTargetInput) {
                            //Calculate value of shares
                            $Value = ($numberOfShares * $currentPriceOfShares);
                            //sell the shares

                            $StartingCapital+=($Value - $TradingCommission);
                            $profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;


							//check if the sell is a winning trade
							if(IsWinning($profitLoss)){
								$WinningTrades+=1;	
							}else{$LosingTrades+=1;}
							
                            //stop multiple transactions on the same day:
                            $Transactiondate = $thedate;
                            $lastTransactionType = "Sell";
                            TableOutput($thedate, "Long Target", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
							//add all the things to the arrays to send to the javascript graphs
						$Value= $currentPriceOfShares*$numberOfShares;
							array_push($gainArray,$profitLoss);
							array_push($profitLossArray,$Value);
							array_push($numberOfSharesArray,$numberOfShares);	
							array_push($dates,$thedate);
							array_push($signalsArray,"Long Target");
                            $numberOfShares = 0;
                            $canBuyShares = true;
                        }
                        break;
                    case "LongIndicator":
                        //STOP Long Indicator 	
						if($LongTarget=="LongIndicator"){
							switch ($LongIndicatorSell1_2) {
								case "Above":
				
									//Sell1_1>Sell1_3
									if ($LongIndicatorSell1 > $LongIndicatorSell3) {
										//Calculate value of shares
										$Value = ($numberOfShares * $currentPriceOfShares);
										//sell the shares
										$StartingCapital+=($Value - $TradingCommission);
										$profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;

										
										//check if the sell is a winning trade
											if(IsWinning($profitLoss)){
												$WinningTrades+=1;	
											}else{$LosingTrades+=1;}
										
										//stop multiple transactions on the same day:
										$Transactiondate = $thedate;
										$lastTransactionType = "Stop Long";
										TableOutput($thedate, "Stop Long", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
										//add all the things to the arrays to send to the javascript graphs
										$Value= $currentPriceOfShares*$numberOfShares;
										array_push($gainArray,$profitLoss);
										array_push($profitLossArray,$Value);
										array_push($numberOfSharesArray,$numberOfShares);	
										array_push($dates,$thedate);
										array_push($signalsArray,"Stop Long");
										$numberOfShares = 0;
										$canBuyShares = true;
									}
									break;
								case "Below":
									//Sell1_1<Sell1_3
				
									if ($LongIndicatorSell1 < $LongIndicatorSell3) {
				
										//Calculate value of shares
										$Value = ($numberOfShares * $currentPriceOfShares);
										//sell the shares
										$StartingCapital+=($Value - $TradingCommission);
				
				
										$profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;

										//check if the sell is a winning trade
											if(IsWinning($profitLoss)){
												$WinningTrades+=1;	
											}else{$LosingTrades+=1;}		
													
										//stop multiple transactions on the same day:
										$Transactiondate = $thedate;
										$lastTransactionType = "Stop Long";
										TableOutput($thedate, "Stop Long", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
										//add all the things to the arrays to send to the javascript graphs
										$Value= $currentPriceOfShares*$numberOfShares;
										array_push($gainArray,$profitLoss);
										array_push($profitLossArray,$Value);
										array_push($numberOfSharesArray,$numberOfShares);	
										array_push($dates,$thedate);
										array_push($signalsArray,"Stop Long");
										$numberOfShares = 0;
										$canBuyShares = true;
									}
									break;
								case "AtLeast%Above%":
									$percentage = CalculatePercentage($LongIndicatorSell1, $LongIndicatorSell3);
									
			
									if ($percentage > $LongIndicatorSellInput1_2) {
				
										//Calculate value of shares
										$Value = ($numberOfShares * $currentPriceOfShares);
										//sell the shares
				
										$StartingCapital+=($Value - $TradingCommission);
				
										$profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;
										
										//check if the sell is a winning trade
											if(IsWinning($profitLoss)){
												$WinningTrades+=1;	
											}else{$LosingTrades+=1;}
										
										//stop multiple transactions on the same day:
										$Transactiondate = $thedate;
										$lastTransactionType = "Stop Long";
										TableOutput($thedate, "Stop Long", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
										//add all the things to the arrays to send to the javascript graphs
										$Value= $currentPriceOfShares*$numberOfShares;
										array_push($gainArray,$profitLoss);
										array_push($profitLossArray,$Value);
										array_push($numberOfSharesArray,$numberOfShares);	
										array_push($dates,$thedate);
										array_push($signalsArray,"Stop Long");
										$numberOfShares = 0;
										$canBuyShares = true;
									}
									break;
								case "AtLeast%Below%":
									$percentage = CalculatePercentage($LongIndicatorSell1, $LongIndicatorSell3);
									if ($percentage <-$LongIndicatorSellInput1_2) {
				
										//Calculate value of shares
										$Value = ($numberOfShares * $currentPriceOfShares);
										//sell the shares
										$StartingCapital+=($Value - $TradingCommission);
				
				
										$profitLoss = ($Value - $TradingCommission) - $initialCostOfShares;
	
										//check if the sell is a winning trade
											if(IsWinning($profitLoss)){
												$WinningTrades+=1;	
											}else{$LosingTrades+=1;}
										
										//stop multiple transactions on the same day:
										$Transactiondate = $thedate;
										$lastTransactionType = "Stop Long";
				
										TableOutput($thedate, "Stop Long", $Symbol, $currentPriceOfShares, $numberOfShares, $Value, $profitLoss);
										//add all the things to the arrays to send to the javascript graphs
										$Value= $currentPriceOfShares*$numberOfShares;
										array_push($gainArray,$profitLoss);
										array_push($profitLossArray,$Value);
										array_push($numberOfSharesArray,$numberOfShares);	
										array_push($dates,$thedate);
										array_push($signalsArray,"Stop Long");
										$numberOfShares = 0;
										$canBuyShares = true;
									}
									break;
							}				
							
						}
                        //do nothing at the moment.
                        break;
                }
            }
        }
        //push the technicals analysis arrays along
        $indexer+=1;
    }

    //end table formating
    echo "<table>";
    $endValue = 0;
	if(isset($lastTransactionType)){
		
    if ($lastTransactionType == "Buy") {
        $endValue = $initialCostOfShares;
    } else {
        $endValue = $StartingCapital;
    }
	}
    $totalreturn = $endValue - $BeginningCapital;
    echo"<table border='1' cellspacing='5' cellpadding='2'>";
    echo"<tr>";
    echo"<td>Start value</td>";
    echo"<td>End value</td>";
    echo"<td>Total return</td>";
	echo"<td>Winning Trades</td>";
    echo"<td>Losing Trades</td>";
	echo"<td>Winning %</td>";
	echo"<td>Min Number of Shares </td>";
    echo"</tr>";
    echo"<tr>";
    echo"<td>$$BeginningCapital</td>";
    echo"<td>$$endValue</td>";
    echo"<td>$$totalreturn</td>";
	echo"<td>$WinningTrades</td>";
	echo"<td>$LosingTrades</td>";
	//prevent any divisions by 0
	if($WinningTrades!=0|$LosingTrades!=0){
	echo"<td>".$WinningTrades/($WinningTrades+$LosingTrades)*100 ."%</td>";
	}else{
		echo"<td>Something whent wrong...</td>";	
	}
	echo"<td>$LockSize</td>";
    echo"</tr>";
    echo"</table>";
} else {

    echo"sorry something whent wrong!";
}

function CalculateProfitPercentage($numberOfShares, $currentPriceOfShares, $TradingCommission, $initialCostOfShares) {
    //set up variables
    $firstCost = $initialCostOfShares;
    $currentPrice = ($numberOfShares * $currentPriceOfShares) + $TradingCommission;
	
	
    //calculation
	
    $profit = $firstCost / $currentPrice;
    $profit = 1 - $profit;
    $profit = $profit * 100;
	
   //return current profit%
    return $profit;
	
}

function CalculatePercentage($Buy1, $Buy3) {

    //calculation
    $percentage = $Buy3 / $Buy1;

    $percentage = 1 - $percentage;
    $percentage = $percentage * 100;

    //return percentage
    return $percentage;
}

function IsWinning($profitLoss){
	if($profitLoss>0){
		return true;	
	}else{
		return false;
	}
		
}



function TableOutput($date, $signal, $symbol, $currentPrice, $numberOfShares, $value, $profitLoss) {
    $red = "#FF3300";
    $green = "#66CC33";
    $nothing = "#FFFFFF";
    $color = "";
    if ($profitLoss == 0) {
        $color = $nothing;
    }
    if ($profitLoss > 0) {
        $color = $green;
    }
    if ($profitLoss < 0) {
        $color = $red;
    }

    
    echo"<tr bgcolor='$color'>";
    echo"<td>$date</td>";
    echo"<td>$signal</td>";
    echo"<td>$symbol</td>";
    echo"<td>$currentPrice</td>";
    echo"<td>$numberOfShares</td>";
    echo"<td>$value</td>";
    echo"<td>$profitLoss</td>";
    echo"</tr>";
    
	
}

?>
    <script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1','packages':['annotationchart']}]}"></script>
    <script type='text/javascript'>
      google.load('visualization', '1', {'packages':['annotationchart']});
      google.setOnLoadCallback(profitLossDrawChart);
	  google.setOnLoadCallback(VolumeDrawChart);
	  
	  	var profitLoss=<?php echo json_encode($profitLossArray); ?>;
		var gain=<?php echo json_encode($gainArray); ?>;
		var time=<?php echo json_encode($dates); ?>;
		var signal=<?php echo json_encode($signalsArray); ?>;
	  
      function profitLossDrawChart() {
		//create the columns
        var data = new google.visualization.DataTable();
        data.addColumn('date', 'Date');
        data.addColumn('number', <?php echo json_encode($Symbol); ?>);
        data.addColumn('string', 'Signals');
		data.addColumn('string', 'Profit');
		console.log("time: "+time.length);	
		
		//for each day:
  		for(i = 0; i < time.length; i++){
	  		//add a row.
			var row=[];
	
			var date= time[i].split("-");
			row.push(new Date(date[0], date[1],date[2]));//date
			row.push(Number(profitLoss[i])); //profit or loss
			row.push(signal[i]); //signal type
			row.push("Profit: "+gain[i]); //amount gained
			data.addRow(row);
		}
		
        var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));

        var options = {
		  displayAnnotations: true,
          title: 'Profit Loss',
          hAxis: {title: 'dates',  titleTextStyle: {color: '#333'}},
          vAxis: {title: 'profit', minValue: 0}
        };

        chart.draw(data, options);
      }
	   
function VolumeDrawChart() {//Volume
	

	  
  var time = <?php echo json_encode($dates); ?>;
  var volumeData=<?php echo json_encode($numberOfSharesArray); ?>;


  var data = new google.visualization.DataTable();
  
  //add the columns
  data.addColumn('string', 'days');
	
  data.addColumn('number',<?php echo json_encode($Symbol); ?>);	
  //foreach day:
  for(i = 0; i < time.length; i++){
	  
	//add row 
	var row=[];
	row.push(time[i]);	//date
	row.push(Number(volumeData[i]));//volume
	data.addRow(row);
	}
		//format the chart
		var Max=Math.max.apply(Math, volumeData);
		var options = {
          title: 'Number of Shares',
          hAxis: {title: 'Day',  titleTextStyle: {color: '#333'}},
          vAxis: {title: 'No. of Shares'},
		     viewWindow: {
        		max:Max,
        		min:0
   			 }
        };

        var chart = new google.visualization.AreaChart(document.getElementById('volumeChart_div'));
        chart.draw(data, options);
      
}
    </script>



  	<h3> Profit and Loss</h3>
    <div id='chart_div' style='width: 1000px; height: 600px;'></div>
    <div id='volumeChart_div' style='width: 1000px; height: 300px;'></div>
    </div>
    <br class="clear" />
  </div>
</div>
<div class="wrapper col6"></div>
<div class="wrapper col7">
  <div id="copyright">
    <p class="fl_left">Copyright &copy; 2011 - All Rights Reserved - <a href="#">Domain Name</a></p>
    <p class="fl_right">Template by <a href="http://www.os-templates.com/" title="Free Website Templates">OS Templates</a></p>
    <br class="clear" />
  </div>
</div>
</body>
</html>
